/*
 * This packet is the initial idenfitication by a client to select the 
 * security level to be used for the rest of the session.
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;

public class UseSecurePacket extends Packet {
    
    public enum SecurityCode { 
        none ((short)1), tls ((short)2);
        public final short value;
        SecurityCode( short value ) {
            this.value = value;
        };
    };
    
    private SecurityCode security_code;
    
    public UseSecurePacket( SecurityCode security_code ) {
        this.security_code = security_code;
        
        /*Set up the header*/
        message_type = MSG_USE_SECURE;
        game_name = 0;
        game_version = 0;
        data_area_size = 4; /*only two shorts in this packet*/
    }
    
    public UseSecurePacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        int val_from_packet = buf.getShort();
        security_code=SecurityCode.class.getEnumConstants()[val_from_packet-1];
    }
	
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        buf.putShort( security_code.value );
        buf.putShort( (short)0 );
        
        return buf.array();
    }
    
    
    public String toString() {
        return super.toString() + "\nsecurity_code = " + security_code.value;
    }
    
    
    public SecurityCode get_security_code() {
        return security_code;
    }
}
